<html>
<head><meta charset="utf-8"><title>UnsafeCell&lt;T&gt; exposes niche(s) of T; should it? #68206 · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html">UnsafeCell&lt;T&gt; exposes niche(s) of T; should it? #68206</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185612087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185612087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185612087">(Jan 14 2020 at 16:26)</a>:</h4>
<p>Interesting question posed on issue <a href="https://github.com/rust-lang/rust/issues/68206" target="_blank" title="https://github.com/rust-lang/rust/issues/68206">#68206</a></p>



<a name="185612310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185612310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185612310">(Jan 14 2020 at 16:28)</a>:</h4>
<p>at least, I find the question of "What kind of 'primitive' is <code>UnsafeCell</code> supposed to be? Something that you are expected to add extra safe-guards to in order to prevent compiler from doing things like niche optimization?</p>



<a name="185612726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185612726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185612726">(Jan 14 2020 at 16:32)</a>:</h4>
<p>I think to me at least it makes sense to not combine UnsafeCell with niches, though it may also be a good argument that using that niche can never be safe as you then can't really abstract around it, with <code>&amp;mut T</code> now no longer making the guarantees it should be able to</p>



<a name="185612766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185612766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185612766">(Jan 14 2020 at 16:32)</a>:</h4>
<p>I cannot tell if your first sentence is stating "<code>UnsafeCell</code> should expose niche" or not</p>



<a name="185612922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185612922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185612922">(Jan 14 2020 at 16:34)</a>:</h4>
<p>(probably because my brain is too willing to freely reinterpret what "combine" might mean here.)</p>



<a name="185633858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185633858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185633858">(Jan 14 2020 at 19:58)</a>:</h4>
<p>ah, I think it should, because generally we don't combine things like that</p>



<a name="185633881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185633881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185633881">(Jan 14 2020 at 19:58)</a>:</h4>
<p>e.g. Arc&lt;Mutex&lt;_&gt;&gt; over SharedMemory&lt;_&gt; basically</p>



<a name="185736102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185736102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185736102">(Jan 15 2020 at 19:44)</a>:</h4>
<p>This <em>sort of vagely</em> relates to the question of dereferenceable</p>



<a name="185736143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185736143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185736143">(Jan 15 2020 at 19:45)</a>:</h4>
<p>that said, I feel like.. hmm.. I'm having memories of discussing this with <span class="user-mention" data-user-id="120791">@RalfJ</span> at last year's all hands :)</p>



<a name="185736172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185736172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185736172">(Jan 15 2020 at 19:45)</a>:</h4>
<p>basically the question is sort of 'what is the escape hatch that says "don't trust these bits"', in part?</p>



<a name="185736195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185736195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185736195">(Jan 15 2020 at 19:45)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="120791">@RalfJ</span> was advocating that this should be <code>union</code> rather than <code>UnsafeCell</code></p>



<a name="185736209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185736209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185736209">(Jan 15 2020 at 19:45)</a>:</h4>
<p>but I do think we should be able to answer the question, regardless</p>



<a name="185736912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185736912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185736912">(Jan 15 2020 at 19:52)</a>:</h4>
<blockquote>
<p>I think <span class="user-mention silent" data-user-id="120791">RalfJ</span> was advocating that this should be <code>union</code> rather than <code>UnsafeCell</code></p>
</blockquote>
<p>what's "this"?</p>



<a name="185737297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185737297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185737297">(Jan 15 2020 at 19:55)</a>:</h4>
<p>"Don't trust these bits" I believe</p>



<a name="185737425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185737425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185737425">(Jan 15 2020 at 19:56)</a>:</h4>
<p>I mean unions already do that (or I think they should, that's <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/73" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/73">https://github.com/rust-lang/unsafe-code-guidelines/issues/73</a>). not sure what niko was saying.^^</p>



<a name="185746390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185746390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185746390">(Jan 15 2020 at 21:31)</a>:</h4>
<p>I think Niko is saying that UnsafeCell should not <em>also</em> do this</p>



<a name="185746406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185746406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185746406">(Jan 15 2020 at 21:31)</a>:</h4>
<p>i.e., if you want this behavior for UnsafeCell then you should also add a union in</p>



<a name="185746453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185746453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185746453">(Jan 15 2020 at 21:32)</a>:</h4>
<p>what is not clear to me is that there are use cases for UnsafeCell where the niche is exposed</p>



<a name="185746486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185746486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185746486">(Jan 15 2020 at 21:32)</a>:</h4>
<p>I think the answer is that all non-threadsafe use cases are fine to expose the niche in, but I'm not confident in that assessment :)</p>



<a name="185781760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185781760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185781760">(Jan 16 2020 at 03:16)</a>:</h4>
<p>What are arguments for not having <code>UnsafeCell</code> <em>also</em> do this? The main one I can imagine is about "expressive power" : someone who knows what they are doing will be able to put the lego-pieces of the type system to precisely express what they want</p>



<a name="185781779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185781779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185781779">(Jan 16 2020 at 03:17)</a>:</h4>
<p>but my Point of View is that out of the box, this is a huge footgun for <code>UnsafeCell</code>, and there is probably too much code out there that was written without thinking about this issue.</p>



<a name="185789351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185789351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185789351">(Jan 16 2020 at 06:27)</a>:</h4>
<blockquote>
<p>what is not clear to me is that there are use cases for UnsafeCell where the niche is exposed</p>
</blockquote>
<p>There's discussion on the issue re. <code>Option&lt;Cell&lt;T&gt;&gt;</code></p>



<a name="185789399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185789399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185789399">(Jan 16 2020 at 06:28)</a>:</h4>
<blockquote>
<p>[...], and there is probably too much code out there that was written without thinking about this issue.</p>
</blockquote>
<p>I am by default intensely skeptical of these types of assertions given past experience.</p>



<a name="185829930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185829930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185829930">(Jan 16 2020 at 16:14)</a>:</h4>
<p>My current opinion is that <code>UnsafeCell&lt;T&gt;</code> should inhibit both</p>
<ul>
<li>niches</li>
<li>dereferencable attribute (when viewed through a reference)</li>
</ul>
<p>We know, after all, that we cannot access the data within without potentially triggering data races.  </p>
<p>But it would be really good to start creating a comprehensive collection of the examples and cases to keep in mind.</p>



<a name="185830024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185830024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185830024">(Jan 16 2020 at 16:15)</a>:</h4>
<p>This feels fairly analogous to me to the auto trait design:</p>



<a name="185830066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185830066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185830066">(Jan 16 2020 at 16:15)</a>:</h4>
<p>We don't consider <code>*mut Send</code>, but you can opt back in at outer levels (except we don't have this opt-in .. yet?)</p>



<a name="185830078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185830078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185830078">(Jan 16 2020 at 16:15)</a>:</h4>
<blockquote>
<blockquote>
<p>[...], and there is probably too much code out there that was written without thinking about this issue.</p>
</blockquote>
<p>I am by default intensely skeptical of these types of assertions given past experience.</p>
</blockquote>
<p>Heh. Not me, I tend to buy those claims when I see them. Only defense Rust has against them is that it is still very young and not as popular as we might like.</p>



<a name="185830083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185830083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185830083">(Jan 16 2020 at 16:15)</a>:</h4>
<p>I'd be inclined to put off the opt-in until we're convinced it's necessary</p>



<a name="185830194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185830194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185830194">(Jan 16 2020 at 16:16)</a>:</h4>
<blockquote>
<p>I think Niko is saying that UnsafeCell should not <em>also</em> do this</p>
</blockquote>
<p>I was not saying that, but I thought I remembered <span class="user-mention" data-user-id="120791">@RalfJ</span> saying it. It sounds like I may have been mistaken.</p>



<a name="185830974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185830974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185830974">(Jan 16 2020 at 16:23)</a>:</h4>
<blockquote>
<p>Heh. Not me, I tend to buy those claims when I see them. Only defense Rust has against them is that it is still very young and not as popular as we might like.</p>
</blockquote>
<p>Whereas I require constructive evidence to believe them. ;) Sometimes it might be the case that some widely used crate does something bad, but then it's also easier to fix centrally.</p>



<a name="185831452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831452">(Jan 16 2020 at 16:27)</a>:</h4>
<p>As far as back-compat is concerned, <code>UnsafeCell&lt;T&gt;</code> is also <code>repr(transparent)</code>, so it seems to me we have already made an observable promise that the niche will be exposed</p>



<a name="185831465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831465">(Jan 16 2020 at 16:27)</a>:</h4>
<p>and folks can rely on that</p>



<a name="185831498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831498">(Jan 16 2020 at 16:28)</a>:</h4>
<p>I spent some time last night trying to review the specs</p>



<a name="185831552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831552">(Jan 16 2020 at 16:28)</a>:</h4>
<p>I couldn't find a place in the reference where the handling of the niche on <code>repr(transparent)</code> on structs was discussed</p>



<a name="185831583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831583">(Jan 16 2020 at 16:28)</a>:</h4>
<p>As a separate point, I do find it elegant, simple, and understandable that <code>UnsafeCell&lt;T&gt;</code> does one thing, not 3 things</p>



<a name="185831605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831605">(Jan 16 2020 at 16:28)</a>:</h4>
<p>and I continue to believe that there is <strong>no reason</strong> to make such an observable promise (from <code>repr(transparent)</code> that you describe.</p>



<a name="185831680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831680">(Jan 16 2020 at 16:29)</a>:</h4>
<blockquote>
<p>I couldn't find a place in the reference where the handling of the niche on <code>repr(transparent)</code> on structs was discussed</p>
</blockquote>
<p>(I <em>did</em> see it in the comments, especially in the <code>repr(transparent)</code> on enum/union RFC PR, but no where regarding the original feature...)</p>



<a name="185831818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831818">(Jan 16 2020 at 16:30)</a>:</h4>
<blockquote>
<p>and I continue to believe that there is <strong>no reason</strong> to make such an observable promise (from <code>repr(transparent)</code> that you describe.</p>
</blockquote>
<p>Though I would be happy to see a, as you put it, constructive example of why such a promise has utility.</p>



<a name="185831852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831852">(Jan 16 2020 at 16:30)</a>:</h4>
<p><code>Option&lt;Cell&lt;T&gt;&gt;</code> was an example</p>



<a name="185831873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831873">(Jan 16 2020 at 16:31)</a>:</h4>
<p>although it was claimed that that occurs infrequently</p>



<a name="185831981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185831981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185831981">(Jan 16 2020 at 16:32)</a>:</h4>
<p>i don't see why the optimization of <code>Option&lt;Cell&lt;T&gt;&gt;</code> needs to be something that follows from <code>repr(transparent)</code>. That strikes me as an implementation detail of <code>Cell&lt;T&gt;</code> itself.</p>



<a name="185832040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832040">(Jan 16 2020 at 16:32)</a>:</h4>
<p>to be honest, though, I'd be perfectly happy with something like <code>#{repr(no_niche)]</code> that we could apply to any ADT (struct/enum/union)</p>



<a name="185832051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832051">(Jan 16 2020 at 16:33)</a>:</h4>
<p>that would be a utility of <code>UnsafeCell&lt;T&gt;</code> exposing niches specifically</p>



<a name="185832209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832209">(Jan 16 2020 at 16:34)</a>:</h4>
<p>and then if that composed to yield <code>#[repr(transparent, no_niche)</code>, then it seems like you would get what you want (<code>#[repr(transparent)]</code> alone has simple semantic interpretation) and I would get what I want (the ability to express the ABI desirata without exposing the niche unnecessarily.</p>



<a name="185832212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832212">(Jan 16 2020 at 16:34)</a>:</h4>
<p><code>repr(transparent)</code> exposing niches feels easy to motivate: just take a newtype wrapper around <code>&amp;'a T</code> and then an <code>Option&lt;_&gt;</code> around that</p>



<a name="185832264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832264">(Jan 16 2020 at 16:34)</a>:</h4>
<blockquote>
<p>and then if that composed to yield #[repr(transparent, no_niche), then it seems like you would get what you want (#[repr(transparent)] alone has simple semantic interpretation) and I would get what I want (the ability to express the ABI desirata without exposing the niche unnecessarily.</p>
</blockquote>
<p>This feels elegant.</p>



<a name="185832305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832305">(Jan 16 2020 at 16:35)</a>:</h4>
<p>Although I wouldn't want to attach it to <code>UnsafeCell&lt;T&gt;</code> necessarily</p>



<a name="185832326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832326">(Jan 16 2020 at 16:35)</a>:</h4>
<p>yeah that we might debate</p>



<a name="185832372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832372">(Jan 16 2020 at 16:35)</a>:</h4>
<p><span class="user-mention" data-user-id="116083">@pnkfelix</span>  but your <code>no_niche</code> feels like "composable language design"</p>



<a name="185832462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832462">(Jan 16 2020 at 16:36)</a>:</h4>
<p>we have a more short-term question of fixing the soundness hole though</p>



<a name="185832467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832467">(Jan 16 2020 at 16:36)</a>:</h4>
<p>yes</p>



<a name="185832492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832492">(Jan 16 2020 at 16:36)</a>:</h4>
<p>use of a niche is itself a compiler optimziation attached to the unstable Rust layout</p>



<a name="185832526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832526">(Jan 16 2020 at 16:37)</a>:</h4>
<p>so I believe we would be within our rights to <em>still</em> make <code>UnsafeCell&lt;T&gt;</code> act as if it has no_niche</p>



<a name="185832538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832538">(Jan 16 2020 at 16:37)</a>:</h4>
<p>for short term</p>



<a name="185832562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832562">(Jan 16 2020 at 16:37)</a>:</h4>
<p>that, or we can go ahead and make a <code>NoNiche&lt;T&gt;</code> wrapper type</p>



<a name="185832576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832576">(Jan 16 2020 at 16:37)</a>:</h4>
<p>and deploy it in libstd where necessary</p>



<a name="185832746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832746">(Jan 16 2020 at 16:38)</a>:</h4>
<p>I'll need to think about whether I agree we are within our rights re. the above discussion re. promises already made (or not made)</p>



<a name="185832757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832757">(Jan 16 2020 at 16:38)</a>:</h4>
<p><code>NoNiche&lt;T&gt;</code> feels more robust as a documentation tool though</p>



<a name="185832810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185832810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185832810">(Jan 16 2020 at 16:39)</a>:</h4>
<p>I'm working on <code>#[repr(no_niche)]</code> in my branch where I'm trying to make <code>UnsafeCell</code> hide its niche. It seemed like a natural thing to poke on while I figure out how to actually accomplish the end goal.</p>



<a name="185833460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185833460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185833460">(Jan 16 2020 at 16:44)</a>:</h4>
<p>Slightly off topic, what does "niche" mean in this context? I tried searching for it, but googling "niche programming" isn't really giving helpful answers. <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="185833561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185833561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185833561">(Jan 16 2020 at 16:45)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> it's the thing that allows <code>Option&lt;&amp;T&gt;</code> to have the same size as <code>&amp;T</code></p>



<a name="185835598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185835598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185835598">(Jan 16 2020 at 17:04)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> more specifically: <code>enum E { A(...), B(...), ... }</code> uses some state called a "discriminant" to differentiate which variant of the enum you are using. A niche allows Rust to store the discriminant intermixed with the payload(s) attached to the variants.</p>



<a name="185836886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185836886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185836886">(Jan 16 2020 at 17:16)</a>:</h4>
<p>There is one relatively authoritative and user-visible sign that <code>repr(transparent)</code> guarantees something about niches: the behavior of the improper_ctypes lint after <a href="https://github.com/rust-lang/rust/pull/60300" target="_blank" title="https://github.com/rust-lang/rust/pull/60300">https://github.com/rust-lang/rust/pull/60300</a><br>
In particular, <code>Option&lt;UnsafeCell&lt;&amp;i32&gt;&gt;</code> <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7027b7e22dab5bedeeb9c54679dd4ea7" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7027b7e22dab5bedeeb9c54679dd4ea7">is considered FFI-safe</a> (and same for <code>Cell</code>)</p>



<a name="185838132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185838132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185838132">(Jan 16 2020 at 17:28)</a>:</h4>
<p>I know that many, perhaps most, unsafe code authors <em>assume</em> repr transparent means "the same layout as the underlying type".<br>
(leaving aside the fact that "layout" has no formal rust definition yet)</p>



<a name="185840301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840301">(Jan 16 2020 at 17:49)</a>:</h4>
<blockquote>
<p>As a separate point, I do find it elegant, simple, and understandable that <code>UnsafeCell&lt;T&gt;</code> does one thing, not 3 things</p>
</blockquote>
<p>note however that it turns out that <code>UnsafeCell</code> having niches means <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/204" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/204">it's really hard to define what that one thing is</a></p>



<a name="185840431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840431">(Jan 16 2020 at 17:50)</a>:</h4>
<p>a variant of stacked borrows that correctly models the "one thing <code>UnsafeCell</code> does" <em>with</em> support for exposing niches might be possible, but I think it will require things I would like to strictly avoid -- like "ghost state" that records the actual enum discriminant in some magical place even when in memory, we just see its encoding via some niche. this is exactly the same kind of "ghost state" as the "active union variant", and I did everything I could to make sure we do <em>not</em> have that ;)</p>



<a name="185840494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840494">(Jan 16 2020 at 17:51)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> will you be available for the lang meeting today?</p>



<a name="185840499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840499">(Jan 16 2020 at 17:51)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span>  so, while I agree with not mixing concepts, I also cannot say that I know how to define the concept of interior mutability on its own, ignoring niches</p>



<a name="185840557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840557">(Jan 16 2020 at 17:52)</a>:</h4>
<p>I'm afraid I have tons of stuff to prepare before flying to a conference on Saturday :/</p>



<a name="185840575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840575">(Jan 16 2020 at 17:52)</a>:</h4>
<p>ah; have fun at the conference</p>



<a name="185840596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185840596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185840596">(Jan 16 2020 at 17:52)</a>:</h4>
<p>(and I'll give a talk about Stacked Borrows there ;)</p>



<a name="185841184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185841184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185841184">(Jan 16 2020 at 17:58)</a>:</h4>
<blockquote>
<p>There is one relatively authoritative and user-visible sign that <code>repr(transparent)</code> guarantees something about niches: the behavior of the improper_ctypes lint after <a href="https://github.com/rust-lang/rust/pull/60300" target="_blank" title="https://github.com/rust-lang/rust/pull/60300">https://github.com/rust-lang/rust/pull/60300</a><br>
In particular, <code>Option&lt;UnsafeCell&lt;&amp;i32&gt;&gt;</code> <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7027b7e22dab5bedeeb9c54679dd4ea7" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7027b7e22dab5bedeeb9c54679dd4ea7">is considered FFI-safe</a> (and same for <code>Cell</code>)</p>
</blockquote>
<p>Okay, I can understand the argument that a user might reasonably expect (based on the use of <code>repr(transparent)</code> and the semantics you are ascribing it w.r.t. niches), that <code>Option&lt;UnsafeCell&lt;&amp;i32&gt;&gt;</code> is FFI-safe.</p>
<p>For now I remain internally divided about whether that type <em>should</em> be FFI-safe, which is a slightly different matter.</p>



<a name="185842232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185842232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185842232">(Jan 16 2020 at 18:08)</a>:</h4>
<p>/me wonders if we can/should instead lint for occurrences of <code>UnsafeCell</code> in a type that implements <code>Sync</code>, and if such occurrences also expose a niche, issue a warning.... (this option may have been already discussed in the issue)</p>



<a name="185842326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185842326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185842326">(Jan 16 2020 at 18:09)</a>:</h4>
<p>Personally I am absolutely convinced that it should not be, because I don't think there's good use cases for it and because I think UnsafeCell should not have niches (for the same reason it should inhibit <code>dereferenceable</code>). But as you say, that is a different question.</p>



<a name="185842485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185842485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185842485">(Jan 16 2020 at 18:11)</a>:</h4>
<p>Ralf's point re. the formal model is compelling at any rate.</p>



<a name="185842547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185842547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185842547">(Jan 16 2020 at 18:12)</a>:</h4>
<p>The memory / perf benefits may be small in comparison to that cost</p>



<a name="185850543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185850543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185850543">(Jan 16 2020 at 19:30)</a>:</h4>
<p>Aside: I can't help but think that we would be in less of a mess now if we'd stuck to <code>repr(transparent)</code> being only for passing things by value in FFI, instead of also (ab)using it as a marker indicating identical <em>memory layout</em> on types that never need to be passed by value in FFI.</p>



<a name="185860269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185860269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185860269">(Jan 16 2020 at 21:08)</a>:</h4>
<p>throw it on the edition pile?</p>



<a name="185860622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185860622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185860622">(Jan 16 2020 at 21:10)</a>:</h4>
<p>The discussion in the meeting unanimously resolved in favor of inhibiting niches on <code>UnsafeCell&lt;T&gt;</code></p>



<a name="185869559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185869559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185869559">(Jan 16 2020 at 22:39)</a>:</h4>
<p>ah yes, I had forgotten about <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/204" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/204">rust-lang/unsafe-code-guidelines#204</a>...</p>



<a name="185870709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185870709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185870709">(Jan 16 2020 at 22:53)</a>:</h4>
<blockquote>
<p>Aside: I can't help but think that we would be in less of a mess now if we'd stuck to <code>repr(transparent)</code> being only for passing things by value in FFI, instead of also (ab)using it as a marker indicating identical <em>memory layout</em> on types that never need to be passed by value in FFI.</p>
</blockquote>
<p>Remind me, <span class="user-mention" data-user-id="124289">@rkruppe</span>, when did we decide to alter the meaning? I actually thought <code>#[repr(transparent)]</code> <em>was</em> only used for controlling ABI, and said nothing in particular about memory layout?</p>



<a name="185871113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185871113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185871113">(Jan 16 2020 at 22:58)</a>:</h4>
<p>I started to write-up some notes on the issue <a href="https://hackmd.io/7rk-0h8WTqKQgaWB6aaeaw" target="_blank" title="https://hackmd.io/7rk-0h8WTqKQgaWB6aaeaw">in a hackmd</a>. <span class="user-mention" data-user-id="120791">@RalfJ</span> I was trying to write-up notes about unsafe-code-guidelines#204 and I wanted to see whether you agreed with this summary:</p>
<ul>
<li>Data-races do not affect <code>Option&lt;Cell&lt;u32&gt;&gt;</code>, however niche optimizations in this case can still have surprising effects, as documented in unsafe-code-guidelines#204:<ul>
<li>In this case, the code converts a <code>&amp;Option&lt;Cell&lt;&amp;u32&gt;&gt;</code> into a <code>*mut usize</code> and writes a <code>0</code> to it. This changes the <code>Option</code> from <code>Some</code> to <code>None</code>. This means that, from the caller's point-of-view, if we invoke a function that takes an argument of type <code>&amp;Option&lt;Cell&lt;&amp;u32&gt;&gt;</code> we must assume that this argument may spontaneously change from <code>&amp;Some(_)</code> to <code>&amp;None</code>, even though safe code could not possibly do that.</li>
</ul>
</li>
</ul>
<p>In particular, I agree the code is <em>surprising</em>, but I'm not sure if it can be blamed purely on niches or lack thereof. If I recall, you were arguing for a "infectious" semantics for <code>UnsafeCell</code>, in which having an <code>UnsafeCell</code> inside of a struct means that (from stacked borrows point-of-view) the entire struct is writable via a shared reference. (Though presently the semantics are somewhat different, and extend only through enums.)</p>
<p>In that case, it seems like a <code>&amp;Option&lt;Cell&lt;&amp;u32&gt;&gt;</code> may wind up changing from <code>&amp;Some</code> to <code>&amp;None</code> regardless, right? (Because the discriminant, even if not stored <em>inside</em> the <code>Cell</code>, would still be in the same <em>struct</em>.)</p>



<a name="185871885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185871885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185871885">(Jan 16 2020 at 23:08)</a>:</h4>
<p>The meaning of the attribute hasn't changed, I am just referring to what it's used for. It's always been specified to give ABI compatibility and motivated by that, but as a side effect it also guarantees size+align equal to the non-ZST field (the size part is necessary for ABI compatibility, the align part perhaps not but it's still implied by the RFC). People took note and started using <code>repr(transparent)</code> to document and/or enforce that <code>SomeWrapper&lt;T&gt;</code> has the same memory layout as <code>T</code> -- perhaps for the lack of a better alternative (<code>repr(C)</code> implies a host or other things you may not want or need, and without any <code>repr</code> the layout is technically unspecified).</p>



<a name="185871953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185871953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185871953">(Jan 16 2020 at 23:09)</a>:</h4>
<p>Actually, I believe we <em>did</em> specify it in the UCG</p>



<a name="185871958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185871958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185871958">(Jan 16 2020 at 23:09)</a>:</h4>
<p>which of course aren't formally decided upon</p>



<a name="185871965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185871965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185871965">(Jan 16 2020 at 23:09)</a>:</h4>
<p>but we might want to start moving towards changing that</p>



<a name="185872037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185872037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185872037">(Jan 16 2020 at 23:10)</a>:</h4>
<p><a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#single-field-structs" target="_blank" title="https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#single-field-structs">see this section</a></p>



<a name="185872044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185872044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185872044">(Jan 16 2020 at 23:10)</a>:</h4>
<p>but perhaps I'm confused</p>



<a name="185872278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185872278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185872278">(Jan 16 2020 at 23:13)</a>:</h4>
<p>Oh, indeed. But as you say, UCG isn't normative. Moreover, this part is just a few months old (<a href="https://github.com/rust-lang/unsafe-code-guidelines/pull/164" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/pull/164">https://github.com/rust-lang/unsafe-code-guidelines/pull/164</a>) so UnsafeCell and Cell being transparent predates it.</p>



<a name="185872572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185872572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185872572">(Jan 16 2020 at 23:16)</a>:</h4>
<p>My framing above may be ahistorical, even the author of the RFC wanted to make UnsafeCell transparent -- not to pass UnsafeCells by values, but just to shut up <code>improper_ctypes</code> about <code>UnsafeCell</code> fields of structs passed "by pointer". See e.g. <a href="https://github.com/rust-lang/rfcs/pull/1758#issuecomment-249611805" target="_blank" title="https://github.com/rust-lang/rfcs/pull/1758#issuecomment-249611805">https://github.com/rust-lang/rfcs/pull/1758#issuecomment-249611805</a></p>



<a name="185877476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185877476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185877476">(Jan 17 2020 at 00:41)</a>:</h4>
<p>It seems that "same layout as the inner type" is very useful, should it be introduced as a separate attribute?</p>



<a name="185878816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185878816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185878816">(Jan 17 2020 at 01:08)</a>:</h4>
<p>My current plan is to introduce <code>#[repr(no_niche)]</code> (or maybe <code>#[repr(hide_niche)]</code>), and then <code>#[repr(tranparent, no_niche)]</code> would have the ABI-oriented meaning, while <code>#[repr(transparent)]</code> would have the layout-oriented one.</p>



<a name="185878836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185878836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185878836">(Jan 17 2020 at 01:08)</a>:</h4>
<p>(I just mention this as a way of saying "I think that is a clean way to express these distinct ideas")</p>



<a name="185896500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185896500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185896500">(Jan 17 2020 at 08:30)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> </p>
<blockquote>
<p>In this case, the code converts a &amp;Option&lt;Cell&lt;&amp;u32&gt;&gt; into a *mut usize and writes a 0 to it. This changes the Option from Some to None. This means that, from the caller's point-of-view, if we invoke a function that takes an argument of type &amp;Option&lt;Cell&lt;&amp;u32&gt;&gt; we must assume that this argument may spontaneously change from &amp;Some(_) to &amp;None, even though safe code could not possibly do that.</p>
</blockquote>
<p>It's not just that safe could couldnt do it, it's that this violates the idea that shared references point to immutable data: the discriminant of <code>Option&lt;Cell&lt;i32&gt;&gt;</code> is logically outside the <code>UnsafeCell</code>, so arguably it should be subject to the "does not change" guarantee and the optimizer should be allowed to exploit that. except that Stacked Borrows is location-based, so even in its most precise handling of UnsafeCell, it cannot justify this optimization. Justifying the optimization would require the crazy "shadow discriminant" stuff I mentioned.</p>



<a name="185896550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185896550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185896550">(Jan 17 2020 at 08:31)</a>:</h4>
<blockquote>
<p>In particular, I agree the code is surprising, but I'm not sure if it can be blamed purely on niches or lack thereof. If I recall, you were arguing for a "infectious" semantics for UnsafeCell, in which having an UnsafeCell inside of a struct means that (from stacked borrows point-of-view) the entire struct is writable via a shared reference. (Though presently the semantics are somewhat different, and extend only through enums.)</p>
</blockquote>
<p>Indeed Stacked Borrows currently is less precise about UnsafeCell than a location-based model could be -- but the point is that with <code>Option&lt;Cell&lt;bool&gt;&gt;</code>, even if we made it fully precise, it'd still be "wrong" in some sense.</p>



<a name="185896984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185896984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185896984">(Jan 17 2020 at 08:41)</a>:</h4>
<p>but indeed, with the "infectious" semantics (as it is currently implement for enums -- but structs are still handled precisely), it doesnt really make much of a difference whether <code>Cell</code> exposes niches or not</p>



<a name="185897054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185897054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185897054">(Jan 17 2020 at 08:42)</a>:</h4>
<p>so, yeah, this is certainly not entirely clear-cut. I merely listed this as further evidence that UnsafeCell with niches is "odd" even in the sequential case -- with consequences for potential variants of Stacked Borrows, but indeed without direct ramifications for the current version</p>



<a name="185897093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185897093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185897093">(Jan 17 2020 at 08:43)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Your argument re. active union field &amp; stacked borrows weighed heavily into our decision last night <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="185897172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185897172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185897172">(Jan 17 2020 at 08:45)</a>:</h4>
<p>hm, okay^^. maybe I should have pointed out the part about the "infectious" semantics earlier...<br>
anyway, I need to start doing "real" work now, ttyl</p>



<a name="185897192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185897192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185897192">(Jan 17 2020 at 08:45)</a>:</h4>
<p>have fun</p>



<a name="185903345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185903345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185903345">(Jan 17 2020 at 10:24)</a>:</h4>
<blockquote>
<p>My current plan is to introduce <code>#[repr(no_niche)]</code> (or maybe <code>#[repr(hide_niche)]</code>), and then <code>#[repr(tranparent, no_niche)]</code> would have the ABI-oriented meaning, while <code>#[repr(transparent)]</code> would have the layout-oriented one.</p>
</blockquote>
<p><span class="user-mention" data-user-id="116083">@pnkfelix</span> I'm not sure if this explanation makes sense to me. Whether there are niches is not really call ABI vs memory layout of the type itself, and more about a further property that does not fit under either: for which type constructors <code>Foo&lt;T&gt;</code> does <code>Foo&lt;Transparent&lt;X&gt;&gt;</code> have the same memory layout and/or call ABI as <code>Foo&lt;X&gt;</code>? There are many <code>Foo</code> where this is false even when niches are "propagated" by <code>Transparent</code>, and many <code>Foo</code> where it's true even when niches are "hidden", and it can be relevant to both call ABI and memory layout (cf. <code>Option&lt;UnsafeCell&lt;&amp;T&gt;&gt;</code> being FFI safe or not). So I consider it a related but separate question from whether <code>Transparent&lt;X&gt;</code>has the same "layout" and/or "ABI" (however we define those terms) as <code>X</code>.</p>
<p>I'm also wondering if introducing <code>repr(no_niche)</code> is really better than just special-casing UnsafeCell: is there any other type that would have any reason whatsoever to use it? It seems to me that the inability to exploit niches is inseparable from interior mutability, and while some types with interior mutability (like <code>Cell</code>) might want to opt back into it for performance, there doesn't seem to be any incentive for types <em>without</em> interior mutability to opt out of it.</p>



<a name="185908930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185908930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185908930">(Jan 17 2020 at 11:54)</a>:</h4>
<p>okay, turns out that we don't need to even talk about how "infectious" <code>UnsafeCell</code> are... <a href="https://github.com/rust-lang/rust/issues/68303" target="_blank" title="https://github.com/rust-lang/rust/issues/68303">https://github.com/rust-lang/rust/issues/68303</a> demonstrates that there is a more fundamental problem with niches and <code>UnsafeCell</code> and stacked borrows. And it's blindingly obvious, so I wonder why I didn't see that earlier... probably I'm just stretched too thin so I dont give each thing that comes up the time it needs :/</p>
<p>It's exactly the same thing as with concurrency: a mutable reference gets created, then a conflicting read happens through another pointer (invalidating the mutable reference), and then the (now invalidated) mutable reference gets used</p>



<a name="185909054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185909054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185909054">(Jan 17 2020 at 11:56)</a>:</h4>
<p>well, at least this reaffirms the recommendation I gave yesterday, that interior mutability with niches is a problem even in sequential code -- maybe my intuition saw things I didnt entirely realize yet ;)</p>



<a name="185912397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185912397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185912397">(Jan 17 2020 at 12:46)</a>:</h4>
<p>hm, so maybe I'm failing to understand something, but it feels like in the niches-don't-propagate through UnsafeCell model, then that code should/would pass miri? As the discriminant's place would be different, and the enum there would be essentially equivalent to a discriminant (u8) + a separate struct/union with the (maybe present) RefCell?</p>



<a name="185912438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185912438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185912438">(Jan 17 2020 at 12:47)</a>:</h4>
<p>maybe stacked borrows does not model &amp;(T, UnsafeCell&lt;U&gt;) -&gt; &amp;mut U as retaining &amp;T in the allowed set in some apis though</p>



<a name="185921974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185921974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185921974">(Jan 17 2020 at 14:52)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> thanks for your comments. Did you see what I wrote in <a href="https://hackmd.io/7rk-0h8WTqKQgaWB6aaeaw#What-about-Cell" target="_blank" title="https://hackmd.io/7rk-0h8WTqKQgaWB6aaeaw#What-about-Cell">the hackmd section</a>? That was based on your prior comments, but I think it agrees with what you wrote. (I've not checked out <a href="https://github.com/rust-lang/rust/issues/68303" target="_blank" title="https://github.com/rust-lang/rust/issues/68303">rust-lang/rust#68303</a> yet, I guess I will extend the hackmd once I do...)</p>



<a name="185922031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185922031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185922031">(Jan 17 2020 at 14:53)</a>:</h4>
<p>Oh, ok, I see what <a href="https://github.com/rust-lang/rust/issues/68303" target="_blank" title="https://github.com/rust-lang/rust/issues/68303">rust-lang/rust#68303</a> is about. Yes, very much.</p>



<a name="185922035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185922035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185922035">(Jan 17 2020 at 14:53)</a>:</h4>
<p>This is the same basic point.</p>



<a name="185922061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185922061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185922061">(Jan 17 2020 at 14:53)</a>:</h4>
<p>(But in reverse)</p>



<a name="185922112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185922112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185922112">(Jan 17 2020 at 14:54)</a>:</h4>
<p>That is, here, the problem is that we are reading the <em>discriminant</em> and this is creating a borrow of <em>the payload</em></p>



<a name="185933184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185933184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185933184">(Jan 17 2020 at 16:41)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> thank you for taking the time to prepare the hackmd</p>



<a name="185933189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185933189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185933189">(Jan 17 2020 at 16:41)</a>:</h4>
<p>I have some questions</p>



<a name="185933315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185933315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185933315">(Jan 17 2020 at 16:42)</a>:</h4>
<blockquote>
<p>A &amp;Option&lt;Mutex&lt;bool&gt;&gt;, for example, might be shared between two threads</p>
</blockquote>
<p>IIUC <code>Mutex</code> has a niche, because it contains an <code>UnsafeCell&lt;bool&gt;</code> that has a niche, is that correct ?</p>



<a name="185933746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185933746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185933746">(Jan 17 2020 at 16:46)</a>:</h4>
<p>And two threads can match on the option, using the discriminant, one thread can get ahold of the lock, and write to the <code>bool</code>, which changes the discriminant, and there is a data-race.  OTOH,  <code>&amp;Mutex&lt;Option&lt;bool&gt;&gt;</code> would not be unsound, right ?</p>



<a name="185933830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185933830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185933830">(Jan 17 2020 at 16:46)</a>:</h4>
<p>Since the discriminant is only modified by the thread that has the <code>Mutex</code></p>



<a name="185934090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185934090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185934090">(Jan 17 2020 at 16:48)</a>:</h4>
<p>So a very different approach might be to not make <code>Option&lt;T&gt;: Sync</code> when <code>T: Sync</code>, but only when <code>T: Sync</code> and the option does not store the discriminant within <code>T</code> due to a layout optimizations, but this approach is probably not ok because it would mean that type checking would depend on layout optimizations..</p>



<a name="185934310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185934310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185934310">(Jan 17 2020 at 16:50)</a>:</h4>
<p>Inhibiting niches in <code>UnsafeCell&lt;T&gt;</code> does seem like the best way to fix things</p>



<a name="185934904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185934904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185934904">(Jan 17 2020 at 16:54)</a>:</h4>
<blockquote>
<p>IIUC <code>Mutex</code> has a niche, because it contains an <code>UnsafeCell&lt;bool&gt;</code> that has a niche, is that correct ?</p>
</blockquote>
<p>correct</p>



<a name="185934948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185934948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185934948">(Jan 17 2020 at 16:55)</a>:</h4>
<blockquote>
<p>And two threads can match on the option, using the discriminant, one thread can get ahold of the lock, and write to the <code>bool</code>, which changes the discriminant, and there is a data-race.  OTOH,  <code>&amp;Mutex&lt;Option&lt;bool&gt;&gt;</code> would not be unsound, right ?</p>
</blockquote>
<p>correct</p>



<a name="185934971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185934971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185934971">(Jan 17 2020 at 16:55)</a>:</h4>
<blockquote>
<p>Specifically, the type Option&lt;UnsafeCell&lt;&amp;i32&gt;&gt; is currently considered FFI-safe (and same for Cell), as it would be represented as a Option&lt;&amp;i32&gt; which in turn is a nullable pointer. After this change, that will no longer be the case.</p>
</blockquote>
<p>I think <code>extern</code> blocks should require <code>unsafe</code>, so I'm not to worried about that, the FFI-safe lints are a "best effort" warning, and we have tuned those in the past.</p>



<a name="185935091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185935091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185935091">(Jan 17 2020 at 16:56)</a>:</h4>
<blockquote>
<p>So a very different approach might be to not make <code>Option&lt;T&gt;: Sync</code> when <code>T: Sync</code>, but only when <code>T: Sync</code> and the option does not store the discriminant within <code>T</code> due to a layout optimizations, but this approach is probably not ok because it would mean that type checking would depend on layout optimizations..</p>
</blockquote>
<p>this would also be wildly backwards incompatible</p>



<a name="185944635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944635">(Jan 17 2020 at 18:42)</a>:</h4>
<blockquote>
<p>hm, so maybe I'm failing to understand something, but it feels like in the niches-don't-propagate through UnsafeCell model, then that code should/would pass miri? As the discriminant's place would be different, and the enum there would be essentially equivalent to a discriminant (u8) + a separate struct/union with the (maybe present) RefCell?</p>
</blockquote>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> yes, my hypothesis is that removing niches from UnsafeCell will fix this bug as well. I can try this in Miri once a PR for the niche removal exists.</p>



<a name="185944641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944641">(Jan 17 2020 at 18:42)</a>:</h4>
<p>(well, I'll be travelling next week, so -- after I'm back, probably)</p>



<a name="185944670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944670">(Jan 17 2020 at 18:43)</a>:</h4>
<blockquote>
<p>maybe stacked borrows does not model &amp;(T, UnsafeCell&lt;U&gt;) -&gt; &amp;mut U as retaining &amp;T in the allowed set in some apis though</p>
</blockquote>
<p>I dont understand what you are saying here</p>



<a name="185944681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944681">(Jan 17 2020 at 18:43)</a>:</h4>
<p>Ah okay, so you were talking about current behavior I think</p>



<a name="185944765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944765">(Jan 17 2020 at 18:44)</a>:</h4>
<p>I was trying to say that it seemed like once the discriminant was not in a niche then things should work just fine</p>



<a name="185944789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944789">(Jan 17 2020 at 18:44)</a>:</h4>
<p>Which I think we agree with</p>



<a name="185944852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944852">(Jan 17 2020 at 18:45)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> well, I had a brief phase of "you are right that the 'infectious' UnsafeCell almost make this niche thing a moot point for Stacked Borrows", followed by that miri issue and realizing "nope the point is not moot at all, this is important and yes the niches should go, even for sequential code".<br>
when I saw that miri issue, things seemed so obvious that I really wonder why I didnt see it before... it was right there in front of my eyes whenever I had niche trouble with Stacked Borrows, but I was blinded because our formal models all dont implement niche optimizations, and I probably thought too much like them. I think that should teach me something, I'll have to reflect on it.</p>



<a name="185944959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185944959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185944959">(Jan 17 2020 at 18:47)</a>:</h4>
<blockquote>
<p>Ah okay, so you were talking about current behavior I think</p>
</blockquote>
<p>yes I was -- I should probably be more clear when there are many candidate models floating around^^</p>



<a name="185994598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/185994598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#185994598">(Jan 18 2020 at 13:03)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> FWIW, I feel the same way about <a href="https://github.com/rust-lang/rust/issues/57893" target="_blank" title="https://github.com/rust-lang/rust/issues/57893">#57893</a> -- I remember thinking to myself when implementing the current checks years ago "this seems so simplistic; can this really suffice?". Ah well. Anyway, I extended the <a href="https://hackmd.io/7rk-0h8WTqKQgaWB6aaeaw#A-deeper-conflict" target="_blank" title="https://hackmd.io/7rk-0h8WTqKQgaWB6aaeaw#A-deeper-conflict">hackmd section</a> on the "deeper conflict" and I <em>think</em> I captured what's going on.</p>
<p>I admit to some discomfort still about how to square that text with the idea that we may sometimes expand the "scope" of struct affected by an <code>UnsafeCell</code>.</p>



<a name="186628949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/186628949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#186628949">(Jan 26 2020 at 16:57)</a>:</h4>
<blockquote>
<p>I admit to some discomfort still about how to square that text with the idea that we may sometimes expand the "scope" of struct affected by an UnsafeCell.</p>
</blockquote>
<p>hm... that is entirely orthogonal though? the latest counterexample doesnt interact with that "scope expansion" at all</p>



<a name="186758042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/186758042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#186758042">(Jan 28 2020 at 07:09)</a>:</h4>
<p>Ah, <span class="user-mention" data-user-id="120791">@RalfJ</span>, I think I see your point. The point is, in <a href="https://github.com/rust-lang/rust/issues/68303" target="_blank" title="https://github.com/rust-lang/rust/issues/68303">#68303</a>, the problem is the <code>&amp;</code> borrow "re-asserting" a shared-lock (at least that's how I think about it =)), versus the other examples where we have the cell permitting surprise mutations. Yes, makes sense. I feel better now.</p>
<p>I guess more generally we have to remember that things "run both ways" -- i.e., we were focused on how <code>Cell</code> permits mutation, but the same memory overlap can <em>also</em> be used by <em>safe code</em> to establish borrows.</p>



<a name="186801911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/186801911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#186801911">(Jan 28 2020 at 16:54)</a>:</h4>
<p>we don't have literal locks any more, that was an old proposal from my first internship -- but, yes :D</p>



<a name="187278991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/UnsafeCell%3CT%3E%20exposes%20niche%28s%29%20of%20T%3B%20should%20it%3F%20%2368206/near/187278991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/UnsafeCell.3CT.3E.20exposes.20niche(s).20of.20T.3B.20should.20it.3F.20.2368206.html#187278991">(Feb 03 2020 at 16:36)</a>:</h4>
<p>Yes, I know. I still think of it as a kind of lock. =)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>